(()=>{
	const IDS = {
		PAGE_CONTENT: '#page_content',
		PAGE_TITLE: '#page_title',
		PAGE_EXPORT: '#page_export',
		CATEGORY_SIDEBAR: '#left_dir_tree',
		ADD_BLOCK: '#add_block',
	};
	window.CoreHelper = class CoreHelper {
		static alertError(msg, buttonName) {
			buttonName = buttonName?buttonName:'好的';
			
			layer.open({
				title:'',
				icon:2,
				btn:[buttonName],
				closeBtn:0,
				content: msg
			});
		}
		
		static msg(msg) {
			layer.msg(msg, {offset:['85%']});
		}
		
		static get PageContentElement() {
			return $(IDS.PAGE_CONTENT);
		}
		
		static get PageTitleElement() {
			return $(IDS.PAGE_TITLE);
		}
		
		static get PageExportElement() {
			return $(IDS.PAGE_EXPORT);
		}
		
		static get CategorySidebarElement() {
			return $(IDS.CATEGORY_SIDEBAR);
		}
		
		static get AddBlockElement() {
			return $(IDS.ADD_BLOCK);
		}
		
		static getBlockElement(id) {
			return $(`#block_${id}`);
		}

		static getBlockValueElement(id) {
			return $(`#block_value_${id}`);
		}

		static getBlockLabelElement(id) {
			return $(`#block_label_${id}`);
		}
		
		static copyElementValue(elm, attr, notip) {
			if(!attr) {
				attr = 'innerHTML';
			}

			let value = elm.prop(attr);
			
			if(attr != 'value') {
				value = Core.htmlDecode(value);
			}
			
			Core.copyText(value);
			
			if(!notip) {
				let preview = value.substr(0, 15);

				if(value.length > 15) {
					preview += '...';
				}

				CoreHelper.msg(`已复制 <span style="color:lightgreen;">${Core.htmlEncode(preview)}</span> 共${value.length}个字符`);
			}
			
			return value;
		}
		
		static copyValueAndTips(value) {
			value = Core.htmlDecode(value);
			Core.copyText(value);

			let preview = value.substr(0, 15);

			if(value.length > 15) {
				preview += '...';
			}
			
			CoreHelper.msg(`已复制 <span style="color:lightgreen;">${Core.htmlEncode(preview)}</span> 共${value.length}个字符`);
		}
		
		static openUrl(url) {
			url = url.replace(/^\s+/, '').replace(/\s+$/, '');
			
			if(url != '') {
				if(!/^[a-zA-Z]+:\/\//.test(url)) {
					url = "http://" + url;
				}
				
				nw.Shell.openExternal(url)
			}
		}
	};
	
	Core.setToParentWindow('CoreHelper', CoreHelper);
})();